﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using App.Data;

namespace Task
{
    public partial class ESI_TBConfigCopy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtProcessName.Text = Request.QueryString["ProcessName"];
                hidStepName.Value = Request.QueryString["StepName"];


                BindDropdownlist();
                if (!string.IsNullOrEmpty(hidStepName.Value))
                {
                    for (int i = 0; i < ddlStepName.Items.Count; i++)
                    {
                        if (ddlStepName.Items[i].Text.ToUpper().Trim() == hidStepName.Value.ToUpper().Trim())
                        {
                            ddlStepName.Items[i].Selected = true;
                        }
                    }
                }

                LoadMapFromDB();
            }
        }

        void LoadMapFromDB()
        {

            DataTable dt = DataAccess.Instance("BusinessDB").ExecuteDataTable ("select * from T_ESI_TB_CONFIG where  ProcessName='" + txtProcessName.Text + "' and StepName='" + ddlStepName.SelectedValue + "'");
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    List<MappingClass> map = MappingClassInstance;
                    map.Add(new MappingClass(row["Table_Name"].ToString(), row["Column_Name"].ToString(), row["SSL_VarName"].ToString()));
                    MappingClassInstance = map;
                }
            }
            else
            {
                MappingClassInstance = new List<MappingClass>();
            }
        }


        void BindDropdownlist()
        {
            //step

            DataTable dt = DataAccess.Instance().ExecuteDataTable("select top 1 PROCESSVERSION,MAINSS from PROCESSES where  ProcessName='" + txtProcessName.Text + "' order by PROCESSVERSION desc");
            if (dt != null && dt.Rows.Count > 0)
            {
                dt = DataAccess.Instance().ExecuteDataTable("select '' as STEPLABEL union all select STEPLABEL from PROCESSSTEPS where  ProcessName='" + txtProcessName.Text + "' and  PROCESSVERSION=" + dt.Rows[0][0].ToString());
                ddlStepName.DataTextField = "STEPLABEL";
                ddlStepName.DataValueField = "STEPLABEL";
                ddlStepName.DataSource = dt;
                ddlStepName.DataBind();

                DataTable dt2 = dt.Copy();
                ddlDestStepName.DataTextField = "STEPLABEL";
                ddlDestStepName.DataValueField = "STEPLABEL";
                ddlDestStepName.DataSource = dt;
                ddlDestStepName.DataBind();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ESI_TBConfigList.aspx?ProcessName=" + txtProcessName.Text);
        }


        List<MappingClass> MappingClassInstance
        {
            get
            {
                if (ViewState["MappingClass"] == null)
                {
                    ViewState["MappingClass"] = new List<MappingClass>();
                }
                try
                {
                    return (List<MappingClass>)ViewState["MappingClass"];
                }
                catch
                {
                    return new List<MappingClass>();
                }
            }
            set
            {
                ViewState["MappingClass"] = value;
            }
        }



        [Serializable]
        public class MappingClass
        {

            public MappingClass(string tableName, string columnName, string varName)
            {
                TableName = tableName;
                ColumnName = columnName;
                VarName = varName;
            }

            string _tableName;

            public string TableName
            {
                get { return _tableName; }
                set { _tableName = value; }
            }

            private string _columnName;

            public string ColumnName
            {
                get { return _columnName; }
                set { _columnName = value; }
            }

            private string _varName;

            public string VarName
            {
                get { return _varName; }
                set { _varName = value; }
            }

            string _id = Guid.NewGuid().ToString();

            public string Id
            {
                get { return _id; }

            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ddlDestStepName.SelectedValue))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('目标步骤名称不能为空');</script>");
                return;
            }
            if (ddlStepName.SelectedValue == ddlDestStepName.SelectedValue)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('目标步骤名称与源步骤名称不能相同');</script>");
                return;
            }

            
            string sql = "delete from T_ESI_TB_CONFIG where ProcessName='{0}' and StepName='" + ddlDestStepName.SelectedValue + "'; insert into T_ESI_TB_CONFIG(GUID, PROCESSNAME,  STEPNAME,  TABLE_NAME, " +
                    "COLUMN_NAME,  SSL_VARNAME, IS_SUBTABLE,  IS_ENABLED ) Select newid(), PROCESSNAME,  '" + ddlDestStepName.SelectedValue + "',  TABLE_NAME, " +
                    "COLUMN_NAME,  SSL_VARNAME, IS_SUBTABLE,  IS_ENABLED from T_ESI_TB_CONFIG where ProcessName='{0}' and StepName='{1}';";


            sql = string.Format(sql, txtProcessName.Text, ddlStepName.SelectedValue);
            DataAccess.Instance("BusinessDB").ExecuteNonQuery(sql);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('复制成功');</script>");
        }

        protected void ddlStepName_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadMapFromDB();
        }
    }
}